package com.myschool.server.web.controller;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.myschool.server.manager.UserManager;
import com.myschool.server.model.Address;
import com.myschool.server.model.Json;
import com.myschool.server.model.Rows;
import com.myschool.server.model.User;
import com.myschool.server.model.RegistrationStatus;
import com.myschool.server.model.UserType;

@Controller
public class TeacherController {

	@Autowired
	UserManager userManager;

	@RequestMapping("getTeachers.htm")
	public @ResponseBody
	Json createTeacher(HttpServletRequest request, String sidx, String sord,
			int page, int rows, Boolean _search, String searchField,
			String searchOper, String searchString) {

		Json jsonData = new Json();
		try

		{

			List<User> users = userManager.retrieveUserByType(UserType.TEACHER); 
			int userCount = users.size();

			System.out.println("Teacher count : " + String.valueOf(userCount));
			jsonData.page = page;
			jsonData.total = userCount;
			jsonData.record = userCount;

			jsonData.rows = new Rows[userCount];
			for (int count = 0; count < userCount; count++) {
				User user = users.get(count);
				Rows row = new Rows();
				row.i = count + 1;
				row.cell = new String[] { user.getEntityId().toString(),
						user.getFirstName(), user.getLastName() };
				jsonData.rows[count] = row; 
			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

		return jsonData;
	}

	@RequestMapping("addTeacher.htm")
	public ModelAndView addTeacher() {
		User user = new User();
		user.setFirstName("Test"); 

		return new ModelAndView("addTeacher", "user", user);

	}

	@RequestMapping("addEditTeacher.htm")
	public void addUser(HttpServletRequest request, HttpServletResponse response) {
		try {

			String fistNemat = request.getParameter("firstName");
			String operation = request.getParameter("oper");
			User user = new User();
			user.setFirstName(fistNemat);
			user.setUsername(fistNemat);
			user.setLastName(request.getParameter("lastName"));
			Address address = new Address();
			
		    
			user.setRegistrationStatus(RegistrationStatus.PR);
			user.setType(UserType.TEACHER);
			
			if (operation.trim().equals("add")) { 
				this.userManager.create(user);
			} else {
				String idStr = request.getParameter("userid");
				Long userID = Long.parseLong(idStr);
				user.setEntityId(userID);
				this.userManager.update(user);
			}
			 
		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

	}

	@RequestMapping("deleteTeacher.htm")
	public void deleteUser(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String userIDParameter = request.getParameter("userID"); 
			String[] userIDArray = userIDParameter.split(";");
			User user;
			for (String userID : userIDArray) {
				if (userID == "")
					continue;
				
				user = new User();
				Long id = Long.parseLong(userID.trim());
				user.setEntityId(id);
				this.userManager.delete(user);
			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

	}

	@RequestMapping("editTeacher.htm")
	public void editUser(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Enumeration values = request.getAttributeNames();

			String fistNemat = request.getParameter("firstName");
			String idStr = request.getParameter("userid");
			Long userID = Long.parseLong(idStr);
			User user = new User();
			user.setFirstName(fistNemat);
			user.setUsername(fistNemat);
			user.setLastName(request.getParameter("lastName"));
			user.setRegistrationStatus(RegistrationStatus.PR);
			user.setType(UserType.TEACHER);
			user.setEntityId(userID);
			this.userManager.update(user);
		
		 
		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

	}

}
